﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class usercontrol_ProductByID : System.Web.UI.UserControl
{
	private int cateid;
	public int CategoryID 
	{
		get { return cateid; }
		set { cateid = value; }
	}
	
	Product product = new Product();
	DaiCuongMobile.SelectedProductDataTable selectedProductTable = new DaiCuongMobile.SelectedProductDataTable(); //san pham da chon mua.
	DaiCuongMobile.SelectedProductRow selectedProductRow;

	protected void Page_Load(object sender, EventArgs e)
	{
		lblEmpty.Visible = false;
		if (!Page.IsPostBack)
		{
			DaiCuongMobile.tbCategoriesDataTable dtCategory = new DaiCuongMobile.tbCategoriesDataTable();
			DaiCuongMobileTableAdapters.tbCategoriesTableAdapter adtCategory = new DaiCuongMobileTableAdapters.tbCategoriesTableAdapter();
			DaiCuongMobile.tbProductsDataTable dtProduct = new DaiCuongMobile.tbProductsDataTable();
			DaiCuongMobileTableAdapters.tbProductsTableAdapter adtProduct = new DaiCuongMobileTableAdapters.tbProductsTableAdapter();

			dtCategory = adtCategory.GetCategoryByCateID(cateid);
			lblTitleGrid.Text = "SẢN PHẨM >> " + dtCategory.Rows[0]["CategoryName"].ToString();
			dtProduct = adtProduct.GetProductByCateID(CategoryID);
			if (dtCategory.Rows.Count > 0)
			{
				DLProducts.DataSource = dtProduct;
				DLProducts.DataBind();
			}
			else
			{
				lblEmpty.Text = "Dữ liệu đang được cập nhật...";
				lblEmpty.Visible = true;
			}
		}

		if ((DaiCuongMobile.SelectedProductDataTable)Session["selectedProduct"] != null)
		{
			selectedProductTable = (DaiCuongMobile.SelectedProductDataTable)Session["selectedProduct"];
		}			
	}
	protected void DLProducts_ItemCommand(object source, DataListCommandEventArgs e)
	{
		if (e.CommandName == "Select")
		{
			int index = 0;
			if (selectedProductTable.Rows.Count > 0)
			{
				index = selectedProductTable.Rows.Count;
				index++;
			}
			else
			{
				//index = Convert.ToInt32(e.CommandArgument.ToString());
				index++;
			}
			product.ProductID = e.CommandArgument.ToString();
			product.ProductName = ((HyperLink)e.Item.FindControl("hplTenSanPham")).Text;
			Label price = (Label)e.Item.FindControl("lblGia");
			product.SalePrice = Convert.ToDecimal(price.Text.Substring(0, price.Text.Length - 3));
			product.Quantity = 1;
			product.Discount = 0;

			// Them vao gio hang
			try
			{
				selectedProductRow = selectedProductTable.NewSelectedProductRow();
				selectedProductRow["Index"] = index;
				selectedProductRow["ProductID"] = product.ProductID;
				selectedProductRow["ProductName"] = product.ProductName;
				selectedProductRow["SalePrice"] = product.SalePrice;
				selectedProductRow["Quantity"] = product.Quantity;
				selectedProductRow["Discount"] = product.Discount;
				selectedProductRow["Amount"] = product.Amount;

				// kiem tra da co trong gio hang san pham nay chua
				/*BanHangOnline.SelectedProductDataTable tbSelectedProducts=selectedProductTable;
				BanHangOnline.SelectedProductRow SelectedProduct;
				for(int i=0;i<=selectedProductTable.Rows.Count-1;i++)
				{
					if (product.ProductID == selectedProductRow.ProductID)
					{
                    
					}
 
				}*/

				selectedProductTable.AddSelectedProductRow(selectedProductRow);
				Session["selectedProduct"] = selectedProductTable;
			}
			catch (Exception)
			{

			}
		}
	}
	protected void DLProducts_ItemDataBound(object sender, DataListItemEventArgs e)
	{
		//Image img = (Image)e.Item.FindControl("imgProduct");
		//img.Attributes.Add("OnMouseOver","Tip('<%#"+ Eval('"'ProductNam'"') +" %>')");
	}
}